﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.Runtime.Serialization;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ServiceModel.DomainServices.Server;
using NetBay.Core.BusinessEntities.Enum;
using NetBay.Core.BusinessEntities.Resources;

#endregion

namespace NetBay.Core.BusinessEntities
{

    /// <summary>
    /// DATABASE TABLENAME : r_paramch
    /// </summary>
    [DataContract]
    public class Index : ReturnObject
    {
        #region Private members

        /// <summary>
        /// Base identifier
        /// </summary>
        private long m_lIndentifier;

        /// <summary>
        /// Name of the table of the index
        /// </summary>
        private string m_sName;

        /// <summary>
        /// Number of the index
        /// </summary>
        private string m_sIndexOrder;

        /// <summary>
        /// Label of the index
        /// </summary>
        private string m_sIndexLabel;

        /// <summary>
        /// Is this index visible
        /// </summary>
        private bool m_bIsVisible;

        /// <summary>
        /// Is this index mandatory
        /// </summary>
        private bool m_bIsMandatory;

        /// <summary>
        /// Tpe of the Index
        /// </summary>
        private int m_iType;

        /// <summary>
        /// Default value of the index
        /// </summary>
        private string m_sDefaultValue;

        /// <summary>
        /// Is the indx Case sensitive
        /// </summary>
        private bool m_bIsCaseSensitive;


        /// <summary>
        /// The char separator
        /// </summary>
        private string m_sSeparator;

        /// <summary>
        /// Max Length of the value
        /// </summary>
        private Int64 m_sValueMaxLength;

        /// <summary>
        /// Number of decimals
        /// </summary>
        private int m_iDecimal;


        //[Obsolete("This field is not used anymore")]
        //private string m_sForbiddenCharacter;

        /// <summary>
        /// Category of the index
        /// </summary>
        private Category m_oCategory = new Category();

        /// <summary>
        /// Is the index unique
        /// </summary>
        private Boolean m_bIsUnique;


        #endregion

        #region Properties
        [DataMember]
        public Boolean IsUnique
        {
            get { return m_bIsUnique; }
            set { m_bIsUnique = value; }
        }

        [DataMember]
        [Required(ErrorMessageResourceType = typeof(Global), ErrorMessageResourceName = "LengthRequired")]
        public long ValueMaxLength { get; set; }

        [DataMember]
        [Required(ErrorMessageResourceType = typeof(Global), ErrorMessageResourceName = "NameRequired")]
        public string IndexLabel { get; set; }

        [Association("Category1", "ForeignKey1", "Key")]
        [Include]
        [DataMember]
        public Category Category
        {
            get { return m_oCategory; }
            set
            {
                m_oCategory = value;
                ForeignKey1 = m_oCategory.Key;
            }
        }

        [DataMember]
        public long Indentifier
        {
            get { return m_lIndentifier; }
            set { m_lIndentifier = value; }
        }

        [DataMember]
        public int Rank { get; set; }

        [DataMember]
        public string Name
        {
            get { return m_sName; }
            set { m_sName = value; }
        }

        [DataMember]
        public string IndexOrder
        {
            get { return m_sIndexOrder; }
            set { m_sIndexOrder = value; }
        }

        [DataMember]
        public bool IsMandatory
        {
            get { return m_bIsMandatory; }
            set { m_bIsMandatory = value; }
        }

        [DataMember]
        public bool IsVisible
        {
            get { return m_bIsVisible; }
            set { m_bIsVisible = value; }
        }

        [DataMember]
        [Required(ErrorMessageResourceType = typeof(Global), ErrorMessageResourceName = "IndexTypeRequired")]
        public int Type { get; set; }

        [DataMember]
        public string DefaultValue
        {
            get { return m_sDefaultValue; }
            set { m_sDefaultValue = value; }
        }

        [DataMember]
        public bool IsCaseSensitive
        {
            get { return m_bIsCaseSensitive; }
            set { m_bIsCaseSensitive = value; }
        }

        [DataMember]
        public string Separator
        {
            get { return m_sSeparator; }
            set { m_sSeparator = value; }
        }

        [DataMember]
        public int Decimal
        {
            get { return m_iDecimal; }
            set { m_iDecimal = value; }
        }

        [DataMember]
        public bool CurrentDateByDefault { get; set; }

        [DataMember]
        public int IncrementalLength { get; set; }

        [DataMember]
        public string IncrementedValue { get; set; }

        /// <summary>
        /// Gets or sets the label rank.
        /// </summary>
        /// <value>The label rank.</value>
        [DataMember]
        public Nullable<int> LabelRank { get; set; }

        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        /// <value>The value.</value>
        [DataMember]
        public string Value { get; set; }

        #endregion

        /// <summary>
        /// Gets the type of the index.
        /// </summary>
        /// <returns></returns>
        public IndexType GetIndexType()
        {
            return (IndexType)System.Enum.ToObject(typeof(IndexType), this.Type);
        }

        /// <summary>
        /// Determines whether [is index type list].
        /// </summary>
        /// <returns>
        ///   <c>true</c> if [is index type list]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsIndexTypeList()
        {
            return (GetIndexType() == IndexType.ListOfValues || GetIndexType() == IndexType.TypeChrono);
        }

        #region ToString
        /// <summary>
        /// Extend default description of class
        /// </summary>
        /// <returns>description of instance</returns>
        public override string ToString()
        {
            StringBuilder parameters = new StringBuilder("Index< ", 4096);
            string l_oLabel = string.Empty;
            if (string.IsNullOrEmpty(IndexLabel) == false)
                l_oLabel = IndexLabel;
            parameters.Append(l_oLabel); parameters.Append(" > ");
            parameters.Append("Type<"); parameters.Append(Type); parameters.Append("> ");

            return parameters.ToString();
        }

        #endregion

        /// <summary>
        /// Abstract class from ItemObject : Create a new instance of this class that is a copy from this instance
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns>new instance</returns>
        public override object Clone()
        {
            Index l_oRet = new Index();
            l_oRet.Indentifier = Indentifier;
            l_oRet.Name = Name;
            l_oRet.IndexOrder = IndexOrder;
            l_oRet.IndexLabel = IndexLabel;
            l_oRet.IsVisible = IsVisible;
            l_oRet.IsMandatory = IsMandatory;
            l_oRet.ValueMaxLength = ValueMaxLength;
            l_oRet.DefaultValue = DefaultValue;
            l_oRet.IsCaseSensitive = IsCaseSensitive;
            l_oRet.Separator = Separator;
            l_oRet.Decimal = Decimal;
            l_oRet.IsUnique = IsUnique;
            l_oRet.Type = Type;
            l_oRet.LabelRank = LabelRank;

            return l_oRet;
        }

    }

    public abstract class IndexDer : Index
    {

    }
}
